﻿namespace PrismEx.UnityExtensions.Tests.TestDomain
{
    using System;

    using Microsoft.Practices.Unity;

    public class SingleTypeResolutionTestDomain<TType, TEventType> : UnityExtensionsTestDomain
    {
        public SingleTypeResolutionTestDomain()
        {
            this.Container.RegisterInstance(SubscriptionDelegate);
        }

        private Action<TEventType> subscriptionDelegate;

        public TType ResolvedInstance { get; private set; }

        public Action<TEventType> SubscriptionDelegate
        {
            get
            {
                return subscriptionDelegate ?? (subscriptionDelegate = e => SubscriptionDelegateCallCount++);
            }

            set
            {
                subscriptionDelegate = e =>
                    {
                        SubscriptionDelegateCallCount++;
                        value.Invoke(e);
                    };
            }
        }
        
        public int SubscriptionDelegateCallCount { get; private set; }

        public void ResolveInstance()
        {
            ResolvedInstance = this.Container.Resolve<TType>();
        }
    }
}
